package com.systemsjr.mpf.db;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

import com.systemsjr.mpf.account.Account;
import com.systemsjr.mpf.account.AccountInstance;
import com.systemsjr.mpf.expense.Expense;
import com.systemsjr.mpf.expense.ExpenseType;

public class MpfDbOpenHelper extends SQLiteOpenHelper {
	private static final String DATABASE_NAME = "FinanceManager";
	// Database Version
	private static final int DATABASE_VERSION = 1;

	public MpfDbOpenHelper(Context context, String name, CursorFactory factory,
			int version) {
		super(context, name, factory, version);

	}

	public MpfDbOpenHelper(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {

		createAccountTable(db);
		createExpenseTypeTable(db);
		createAccountInstanceTable(db);
		createExpenseTable(db);

	}

	public void createAccountTable(SQLiteDatabase db) {
		db.execSQL(Account.createTable());
	}

	public void createExpenseTable(SQLiteDatabase db) {
		db.execSQL(Expense.createTable());
	}

	public void createExpenseTypeTable(SQLiteDatabase db) {
		db.execSQL(ExpenseType.createTable());
	}

	public void createAccountInstanceTable(SQLiteDatabase db) {
		db.execSQL(AccountInstance.createTable());
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int arg1, int arg2) {
		db.execSQL("DROP TABLE IF EXISTS " + Account.TABLE_NAME);
		db.execSQL("DROP TABLE IF EXISTS " + ExpenseType.TABLE_NAME);
		db.execSQL("DROP TABLE IF EXISTS " + AccountInstance.TABLE_NAME);
		db.execSQL("DROP TABLE IF EXISTS " + Expense.TABLE_NAME);

		onCreate(db);
	}

	@Override
	public void onConfigure(SQLiteDatabase db) {
		try {
			// onCreate(db);
		} catch (Exception e) {
		}
	}

}
